import { api } from '../utils/apiWrapper.js'
import { encryptPassword } from '../utils/crypto'
export async function getUserList(params) {
  return await api.get('/api/data/User', params)
}

async function getUserProjectList() {
  return await api.get('/api/data/UserToPoroject')
} 

export async function getUserProjects(userId) {
  const params = { page: 1, pageSize: 1000, where: { UserId: userId } }
 var res=await getUserProjectList(params)
 
 console.log(res.records);
 return res.records;
}

// 获取单个用户
export  function getUserById(id) {
  return  api.get(`/api/data/User/${id}`)
}

// 创建用户
export  function createUser(data) {
  // 如果包含密码，则加密密码
  if (data.password || data.Password) {
    const password = data.password || data.Password
    const encryptedPassword = encryptPassword(password)
    return api.post('/api/data/User', {
      ...data,
      password: encryptedPassword,
      Password: encryptedPassword
    })
  }
  return api.post('/api/data/User', data)
}

// 更新用户
export  function updateUser(id, data) {
  // 如果包含密码，则加密密码
  if (data.password || data.Password) {
    const password = data.password || data.Password
    const encryptedPassword = encryptPassword(password)
    return api.put(`/api/data/User/${id}`, {
      ...data,
      password: encryptedPassword,
      Password: encryptedPassword
    })
  }
  return api.put(`/api/data/User/${id}`, data)
}

// 删除用户
export  function deleteUser(id) {
  return  api.delete(`/api/data/User/${id}`)
}
export function login(data) {
  return api.post('/api/auth/login', data)
}

